﻿using LoveEat.API.Write.Applications.Commands.Goods;
using LoveEat.Domain.Goods;
using LoveEat.ErrorCode;
using LoveEat.Infrastructure.Interfaces;
using MediatR;

namespace LoveEat.API.Write.Applications.CommandHandlers.Goods
{
    public class DeleteGoodsCommandHandler : IRequestHandler<DeleteGoodsCommand, APIResult<int>>
    {
        private readonly IBaseRespository<GoodsModel> goodsRep;

        public DeleteGoodsCommandHandler(IBaseRespository<GoodsModel> goodsRep)
        {
            this.goodsRep = goodsRep;
        }

        public async Task<APIResult<int>> Handle(DeleteGoodsCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code=APIResultCode.删除成功;
            result.Message=APIResultCode.删除成功.ToString();

            var res=goodsRep.GetAll().FirstOrDefault(x=>x.GoodsId==request.GoodsId);
            res.IsDelete=true;
            await goodsRep.UpdateAsync(res);

            return await Task.FromResult(result);
        }
    }
}
